import { Injectable } from '@nestjs/common';
import { Food, FoodModel } from "../Scheam/food.scheam"
import { Creat_Food } from "../Types/Food"
@Injectable()
export class FoodsService {
  async create(Data: Creat_Food) {
    try {
      const result = await FoodModel.create({ ...Data })
      return result
    } catch (error) {
      console.log(error.message)
      return false
    }
  }
  async findOne(id) {
    try {
      const result = await FoodModel.findOne({ _id: id })
      return result
    } catch (error) {
      return true
    }

  }
  async findAll(skip = 0, type = "4", keyword = "", order = "desc") {
    try {
      if (order == 'desc') {
        if (!keyword) {

          if (type == '4') {
            return {
              data: await FoodModel.find().sort({ "publish_time": "desc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $count: "total"
                }
              ])
            }
          } else {
            return {
              data: await FoodModel.find({
                type: type
              }).sort({ "publish_time": "desc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: { type: type }
                },
                {
                  $count: "total"
                }
              ])
            }
          }
        } else {
          if (type == '4') {
            return {
              data: await FoodModel.find({
                title: { $regex: keyword, $options: 'i' }
              }).sort({ "publish_time": "desc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: { title: { $regex: keyword, $options: 'i' } }
                },
                {
                  $count: "total"
                }
              ])
            }
          } else {
            return {
              data: await FoodModel.find({
                type: type,
                title: { $regex: keyword, $options: 'i' }
              }).sort({ "publish_time": "desc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: {
                    type: type,
                    title: { $regex: keyword, $options: 'i' }
                  }
                },
                {
                  $count: "total"
                }
              ])
            }
          }
        }
      } else {
        if (!keyword) {

          if (type == '4') {
            return {
              data: await FoodModel.find().sort({ "publish_time": "asc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $count: "total"
                }
              ])
            }
          } else {
            return {
              data: await FoodModel.find({
                type: type
              }).sort({ "publish_time": "asc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: { type: type }
                },
                {
                  $count: "total"
                }
              ])
            }
          }
        } else {
          if (type == '4') {
            return {
              data: await FoodModel.find({
                title: { $regex: keyword, $options: 'i' }
              }).sort({ "publish_time": "asc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: { title: { $regex: keyword, $options: 'i' } }
                },
                {
                  $count: "total"
                }
              ])
            }
          } else {
            return {
              data: await FoodModel.find({
                type: type,
                title: { $regex: keyword, $options: 'i' }
              }).sort({ "publish_time": "asc" }).select('-__v').skip(skip).limit(5),
              count: await FoodModel.aggregate([
                {
                  $match: {
                    type: type,
                    title: { $regex: keyword, $options: 'i' }
                  }
                },
                {
                  $count: "total"
                }
              ])
            }
          }
        }
      }



    } catch (error) {
      console.log(error.message)
      return {}
    }
  }

  async deleteOne(id: string) {
    try {
      return FoodModel.deleteOne({ _id: id })
    } catch (error) {
      return false
    }

  }

  async searchlist(keyword) {


    return FoodModel.find({
      title: { $regex: keyword, $options: 'i' }
    }).select('-__v -type -rich_text -catalog -user_id -publish_time')
  }


  async foodlist(skip) {
    return FoodModel.find().skip(skip).limit(9).sort({ "publish_time": "desc" }).select('-__v -type -rich_text -catalog -user_id -publish_time')
  }

  async Recomment() {

    return FoodModel.find().sort({ "review": "desc" }).limit(2).select('-__v -type -rich_text -catalog -user_id -publish_time')
  }
  async Recomment2() {

    return FoodModel.find().sort({ "review": "desc" }).limit(4).select('_id Cover_img')
  }


  async category(skip, type) {
    return await FoodModel.find({
      type: type
    }).skip(skip).limit(12).select('-__v -type -rich_text -catalog -user_id -publish_time')
  }

  async userfood(id, skip = 0) {

    return await FoodModel.find({
      user_id: id
    }).skip(skip).limit(6).sort({ "publish_time": "desc" }).select('-__v -type -rich_text -catalog -publish_time')
  }

  async update(id: string, Data: Creat_Food) {
    try {
      const result = await FoodModel.updateOne({
        _id: id
      }, { ...Data })
      return result
    } catch (error) {
      return false
    }
  }
  async addviews(id: string) {
    try {
      console.log(id, 'sss')
      return await FoodModel.updateOne({
        _id: id
      }, {
        $inc: { review: 1 }

      })
    } catch (error) {
      return false
    }
  }


}
